package com.vehicle.controller;


import com.vehicle.bean.Record;
import com.vehicle.form.AddRecordForm;
import com.vehicle.service.RecordService;
import com.vehicle.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-04-21
 */
@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    RecordService recordService;


    //管理端获取所有停车记录
    @GetMapping("/allList/{page}/{size}")
    public ResultVO getAllList(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
       return ResultVO.succ(recordService.getAllRecordList(page,size));
    }

    //用户端：根据用户id获得用户的停车记录
    @GetMapping("/userList/{userId}")
    public ResultVO getListByUserId(@PathVariable("userId") Integer userId){
        return ResultVO.succ(recordService.getUserRecord(userId));
    }

    @GetMapping("/getTenMessage/")
    public ResultVO getTenMessage(){
        return ResultVO.succ(recordService.getNewTen());
    }

    //用户端：根据用户id获得用户的停车记录
    @GetMapping("/recordDetail/{recordId}")
    public ResultVO getRecordDetail(@PathVariable("recordId") Integer recordId){
        return ResultVO.succ(recordService.getUserDetail(recordId));
    }


    //用户停车就添加记录
    @PostMapping("/setCar")
    public ResultVO userSetCar(HttpServletRequest request, @RequestBody AddRecordForm addRecordForm){
        String ip = request.getRemoteAddr();
        String id = request.getHeader("id");
        return ResultVO.succ(recordService.addRecordListIn(ip,Integer.parseInt(id),addRecordForm));
    }


    //用户还车修改记录
    @PostMapping("/leftCar/{recordId}")
    public ResultVO userReturnCar(HttpServletRequest request,@PathVariable("recordId") Integer recordId){
        String ip = request.getRemoteAddr();
        String id = request.getHeader("id");
        return ResultVO.succ(recordService.addRecordListOut(ip,Integer.parseInt(id),recordId));
    }

    //根据用户id获取用户停车次数
    @GetMapping("/getParkingNumber/{userId}")
    public ResultVO getParkingNumber(@PathVariable("userId") Integer userId){

        return ResultVO.succ(recordService.getNumber(userId));
    }

    //根据用户id获取最后一次停车时间
    @GetMapping("/getParkingTime/{userId}")
    public ResultVO getParkingTime(@PathVariable("userId") Integer userId){

        return ResultVO.succ(recordService.getTime(userId));
    }

    //车是否停在车库中
    @GetMapping("/existIn/{userId}")
    public ResultVO existIn(@PathVariable("userId") Integer userId){
        Record r = recordService.existOrNot(userId);
        if(r == null){
            return ResultVO.fail(",,,");
        }
        return ResultVO.succ(r);
    }

    //车是否停在车库中
    @GetMapping("/existInkU/{parkingId}")
    public ResultVO existInKu(@PathVariable("parkingId") Integer parkingId){
        Record r = recordService.existInKu(parkingId);
        if(r == null){
            return ResultVO.fail(",,,");
        }
        return ResultVO.succ(r);
    }

    //用户是否已有车停在车库中
    @GetMapping("/carExistInkU/{userId}")
    public ResultVO carExistInkU(@PathVariable("userId") Integer userId){
       return ResultVO.succ(recordService.carExistInkU(userId));
    }
}

